package com.example.demo.jpa.controller;

import com.example.demo.jpa.mapper.JPARepository;
import com.example.demo.jpa.mapper.JPAUserRepository;
import com.example.demo.jpa.service.JpaService;
import com.example.demo.model.Material;
import com.example.demo.model.User;
import com.example.demo.model.vo.MaterialAddVo;
import com.example.demo.model.vo.MaterialSelectVo;
import com.example.demo.model.vo.MaterialUpdateVo;
import com.example.demo.model.vo.MaterialVo;
import com.example.demo.util.AllRecords;
import com.example.demo.util.Result;
import com.example.demo.util.ResultVo;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 86151 on 2018/12/25.
 */
@RestController
public class JpaController {

    @Autowired
    private JPARepository jpaRepository;
    @Autowired
    private JPAUserRepository jpaUserRepository;

    @Autowired
    private JpaService jpaService;
    @GetMapping("testJPA")
    public List<Material> testJPA(){
        List<String> codeList = new ArrayList<>();
        codeList.add("BEIJIAN6666");
        codeList.add("BEIJIAN7777");
        List<Material> materialByMaterialCodeIn = jpaRepository.findMaterialByMaterialCodeIn(codeList);
        return materialByMaterialCodeIn;
    }

    @GetMapping("queryAll")
    public Material  add(@RequestParam("id") Integer id){
//        return jpaRepository.findOne(id);
        Material byId = jpaRepository.findById(id);
        System.out.println(byId);

//        jpaRepository.findAll
        return byId;
    }



    @GetMapping("findByMaterialCodeAndMaterialName")
    public List<Material>  add(@RequestParam("code") String code,@RequestParam("name") String name){
          return jpaRepository.findByMaterialCodeAndMaterialName(code,name);

    }

    @GetMapping("queryUserById")
    public User queryUserById(@RequestParam("id") Integer id){
//        return jpaRepository.findOne(id);
        User byId = jpaUserRepository.findById(id);
        System.out.println(byId);
        return byId;
    }
    @PostMapping("add")
    public void add(@RequestBody Material material){
        jpaRepository.save(material);
    }
    @PostMapping("update")
    public void update(@RequestBody Material material){
        jpaRepository.save(material);
    }
    @PostMapping("delete")
    public void delete(@RequestBody Material material){
        jpaRepository.delete(material);
    }

    @PostMapping("queryPage")
    public Result<AllRecords> queryPage(@RequestBody MaterialSelectVo materialSelectVo){
        return jpaService.queryPage(materialSelectVo);
    }

    /**
     * 新增备件小类
     * @param materialAddVo
     * @return
     */
    @PostMapping("addMaterialJpa")
    public Result<MaterialAddVo> addMaterialJpa(@RequestBody MaterialAddVo materialAddVo){
        return jpaService.addMaterialJpa(materialAddVo);
    }
    /**
     * 修改前的回显，查看备件小类信息。
     */
    @GetMapping("queryById")
    public Result<MaterialVo> queryById(@RequestParam("id") Integer id){
        return jpaService.queryById(id);
    }
    /**
     * 修改
     */
    @PostMapping("updateMaterial")
    public Result<MaterialUpdateVo> updateMaterial(@RequestBody @Valid MaterialUpdateVo materialUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVo.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return jpaService.updateMaterial(materialUpdateVo);
    }
}
